%% Fidrmuc J., Kapounek, S., Ku�erov�, Z: Time�Frequency Analysis of Cryptocurrency Attention
% MODWTcorr

%% Table A3 - Bitcoin - frequency scale correlation
close all
clear
clc
cd('... path ...')
load('data02.mat')
clearvars -except btc* g_btc*
T=table('Size',[0 9],'VariableTypes',{'string','string','string','string','string','string','string','string','string'},'VariableNames',{'Exchanges','r2_4','r4_8','r8_16','r16_32','r32_64','r64_128','r128_256','all'}); 
n=length(btcnames);
for i=1:3:n
    % variables definition
    eval(['x=g_btc_' btcnames{i} ';']);
    y=btc{:,i};
    % transformation
    x=diff(x,1);
    y=diff(y,1);
    % NaN values
    A=[x y];
    X=A(~any(isnan(A),2),:);
    x=X(:,1);y=X(:,2);
    clear X A
    % MODWT + correlations
    [scorr,spval] = corrcoef(x,y);
    wtx=modwt(x,'db4',8,'reflection');
    wty=modwt(y,'db4',8,'reflection');
    [wcorr,wcorrci,wpval]=modwtcorr(wtx,wty,'db4',0.95,'reflection');
    wcorr=wcorr(1:7);wpval=(wpval(1:7,2))';    
    corr=[wcorr' scorr(1,2)];
    pvalue=[wpval spval(1,2)];
    star=strings(1,8);star(pvalue<0.1)='*';star(pvalue<0.05)='**';star(pvalue<0.01)='***';
    corr=compose('%.4f',corr);corr=[corr; star];corr=join(corr,"",1);
    % table saving
    name={['SVI diff ("bitcoin ' cell2mat(btcnames(i)) '") - Bitcoin returns (' cell2mat(btcnames(i)) ')']};
    Tnew(1,:)=table(string(name),corr(1),corr(2),corr(3),corr(4),corr(5),corr(6),corr(7),corr(8),'VariableNames',{'Exchanges','r2_4','r4_8','r8_16','r16_32','r32_64','r64_128','r128_256','all'});
    T=[T;Tnew];
end
mkdir('results_final');
cd('results_final');
writetable(T,'tableA3a.dat','Delimiter','\t')
cd('..');
close(gcf);


%% Table A3 - Ethereum - frequency scale correlation
close all
clear
clc
cd('... path ...')
load('data02.mat')
clearvars -except eth* g_eth*
T=table('Size',[0 9],'VariableTypes',{'string','string','string','string','string','string','string','string','string'},'VariableNames',{'Exchanges','r2_4','r4_8','r8_16','r16_32','r32_64','r64_128','r128_256','all'}); 
n=length(ethnames);
for i=1:3:n
    % variables definition
    eval(['x=g_eth_' ethnames{i} ';']);
    y=eth{:,i};
    % transformation
    x=diff(x,1);
    y=diff(y,1);
    % NaN values
    A=[x y];
    X=A(~any(isnan(A),2),:);
    x=X(:,1);y=X(:,2);
    clear X A
    % MODWT + correlations
    [scorr,spval] = corrcoef(x,y);
    wtx=modwt(x,'db4',8,'reflection');
    wty=modwt(y,'db4',8,'reflection');
    [wcorr,wcorrci,wpval]=modwtcorr(wtx,wty,'db4',0.95,'reflection');
    wcorr=wcorr(1:7);wpval=(wpval(1:7,2))';    
    corr=[wcorr' scorr(1,2)];
    pvalue=[wpval spval(1,2)];
    star=strings(1,8);star(pvalue<0.1)='*';star(pvalue<0.05)='**';star(pvalue<0.01)='***';
    corr=compose('%.4f',corr);corr=[corr; star];corr=join(corr,"",1);
    % table saving
    name={['SVI diff ("ethereum ' cell2mat(ethnames(i)) '") - Ethereum returns (' cell2mat(ethnames(i)) ')']};
    Tnew(1,:)=table(string(name),corr(1),corr(2),corr(3),corr(4),corr(5),corr(6),corr(7),corr(8),'VariableNames',{'Exchanges','r2_4','r4_8','r8_16','r16_32','r32_64','r64_128','r128_256','all'});
    T=[T;Tnew];
end
mkdir('results_final');
cd('results_final');
writetable(T,'tableA3b.dat','Delimiter','\t')
cd('..');
close(gcf);

%% Table A3 - Litecoin - frequency scale correlation
close all
clear
clc
cd('... path ...')
load('data02.mat')
clearvars -except ltc* g_ltc*
T=table('Size',[0 9],'VariableTypes',{'string','string','string','string','string','string','string','string','string'},'VariableNames',{'Exchanges','r2_4','r4_8','r8_16','r16_32','r32_64','r64_128','r128_256','all'}); 
n=length(ltcnames);
for i=1:3:n
    % variables definition
    eval(['x=g_ltc_' ltcnames{i} ';']);
    y=ltc{:,i};
    % transformation
    x=diff(x,1);
    y=diff(y,1);
    % NaN values
    A=[x y];
    X=A(~any(isnan(A),2),:);
    x=X(:,1);y=X(:,2);
    clear X A
    % MODWT + correlations
    [scorr,spval] = corrcoef(x,y);
    wtx=modwt(x,'db4',8,'reflection');
    wty=modwt(y,'db4',8,'reflection');
    [wcorr,wcorrci,wpval]=modwtcorr(wtx,wty,'db4',0.95,'reflection');
    wcorr=wcorr(1:7);wpval=(wpval(1:7,2))';    
    corr=[wcorr' scorr(1,2)];
    pvalue=[wpval spval(1,2)];
    star=strings(1,8);star(pvalue<0.1)='*';star(pvalue<0.05)='**';star(pvalue<0.01)='***';
    corr=compose('%.4f',corr);corr=[corr; star];corr=join(corr,"",1);
    % table saving
    name={['SVI diff ("litecoin ' cell2mat(ltcnames(i)) '") - Litecoin returns (' cell2mat(ltcnames(i)) ')']};
    Tnew(1,:)=table(string(name),corr(1),corr(2),corr(3),corr(4),corr(5),corr(6),corr(7),corr(8),'VariableNames',{'Exchanges','r2_4','r4_8','r8_16','r16_32','r32_64','r64_128','r128_256','all'});
    T=[T;Tnew];
end
mkdir('results_final');
cd('results_final');
writetable(T,'tableA3c.dat','Delimiter','\t')
cd('..');
close(gcf);

%% Figure 6 - wavelet cross-correlation sequence - kraken
close all
clear
clc
cd('... path ...')
load('data02.mat')

% transformation and variable definition
X=NaN(2369,3);
X(:,1)=btc{:,13};
X(size(X,1)-size(eth,1)+1:end,2)=eth{:,13};
X(size(X,1)-size(ltc,1)+1:end,3)=ltc{:,13};
X=diff(X,1,1);

Y=NaN(2369,3);
Y(:,1)=g_btc_kraken;
Y(size(Y,1)-size(g_eth_kraken,1)+1:end,2)=g_eth_kraken;
Y(size(Y,1)-size(g_ltc_kraken,1)+1:end,3)=g_ltc_kraken;
Y=diff(Y,1,1);

clearvars -except X Y  
market='kraken';
cnames={'bitcoin','ethereum','litecoin'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
B1=A1(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);
clear X Y A*

% figure creation
close all
figure('units','pixels','Position',[50,100,900,300]);
s1=subplot(1,3,1);
x1 = modwt(B1(:,1),'db4',8,'reflection');
y1 = modwt(B1(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
zerolag8 = floor(numel(xcseq{8})/2)+1;
p1=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-',lags{8}(zerolag8:zerolag8+30),xcseq{8}(zerolag8:zerolag8+30),'k-');
p1(1).LineWidth=0.5;
p1(2).LineWidth=0.5;
p1(3).LineWidth=0.5;
p1(4).LineWidth=0.5;
p1(5).LineWidth=0.75;
p1(5).Marker = '*';
p1(6).LineWidth=0.75;
p1(6).Marker = 'o';
p1(7).LineWidth=0.75;
p1(7).Marker = '+';
p1(8).LineWidth=0.75;
p1(8).Marker = 'd';
xlabel('Lead (days)');
ylabel('Correlation');
ylim([-1 1])
title({['SVI diff ("' cell2mat(cnames(1)) ' ' market '")']});

s2=subplot(1,3,2);
x1 = modwt(B2(:,1),'db4',8,'reflection');
y1 = modwt(B2(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p2=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p2(1).LineWidth=0.5;
p2(2).LineWidth=0.5;
p2(3).LineWidth=0.5;
p2(4).LineWidth=0.5;
p2(5).LineWidth=0.75;
p2(5).Marker = '*';
p2(6).LineWidth=0.75;
p2(6).Marker = 'o';
p2(7).LineWidth=0.75;
p2(7).Marker = '+';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cell2mat(cnames(2)) ' ' market '")']});

s3=subplot(1,3,3);
x1 = modwt(B3(:,1),'db4',8,'reflection');
y1 = modwt(B3(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
zerolag8 = floor(numel(xcseq{8})/2)+1;
p3=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-',lags{8}(zerolag8:zerolag8+30),xcseq{8}(zerolag8:zerolag8+30),'k-');
p3(1).LineWidth=0.5;
p3(2).LineWidth=0.5;
p3(3).LineWidth=0.5;
p3(4).LineWidth=0.5;
p3(5).LineWidth=0.75;
p3(5).Marker = '*';
p3(6).LineWidth=0.75;
p3(6).Marker = 'o';
p3(7).LineWidth=0.75;
p3(7).Marker = '+';
p3(8).LineWidth=0.75;
p3(8).Marker = 'd';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cell2mat(cnames(3)) ' ' market '")']});

% position
s=get(s1, 'position');s(1)=0.06;s(3)=0.27;s(2)=0.25;s(4)=0.69;set(s1,'position',s); % [left bottom width height]
s=get(s2, 'position');s(1)=0.38;s(3)=0.27;s(2)=0.25;s(4)=0.69;set(s2,'position',s);
s=get(s3, 'position');s(1)=0.70;s(3)=0.27;s(2)=0.25;s(4)=0.69;set(s3,'position',s);

% add legend
Lgnd=legend('2-4','4-8','8-16','16-32','32-64','64-128','128-256','256-512','NumColumns',8,'FontSize',10)
Lgnd.Position(1) = 0.12;
Lgnd.Position(2) = 0.02;

mkdir('results_final');
cd('results_final');
%saveas(gcf,'fig06','png');
print(gcf,'fig06','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Fig A04a - wavelet cross-correlation sequence - bitcoin
close all
clear
clc
cd('... path ...')
load('data02.mat')
% transformation and variable definition
n=size(btc,2)/3;
m=length(btc_datelabel)-1;
X=NaN(m,n);
j=1;
for i=1:1:n
    X(:,i)=diff(btc{:,j},1);
    names(:,i)=btcnames(:,j);
    j=j+3;
end
datelabel=btc_datelabel(2:end,:);
Y(:,1)=g_btc_bitfinex;
Y(:,2)=g_btc_bitstamp;
Y(:,3)=g_btc_bittrex;
Y(:,4)=g_btc_coinbase;
Y(:,5)=g_btc_kraken;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
cname='bitcoin';
Cname='Bitcoin';

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);
clear datelabel X Y A*

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
x1 = modwt(B1(:,1),'db4',8,'reflection');
y1 = modwt(B1(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
zerolag8 = floor(numel(xcseq{8})/2)+1;
p1=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-',lags{8}(zerolag8:zerolag8+30),xcseq{8}(zerolag8:zerolag8+30),'k-');
p1(1).LineWidth=0.5;
p1(2).LineWidth=0.5;
p1(3).LineWidth=0.5;
p1(4).LineWidth=0.5;
p1(5).LineWidth=0.75;
p1(5).Marker = '*';
p1(6).LineWidth=0.75;
p1(6).Marker = 'o';
p1(7).LineWidth=0.75;
p1(7).Marker = '+';
p1(8).LineWidth=0.75;
p1(8).Marker = 'd';
xlabel('Lead (days)');
ylabel('Correlation');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(1)) '")']});

s2=subplot(1,5,2);
x1 = modwt(B2(:,1),'db4',8,'reflection');
y1 = modwt(B2(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
zerolag8 = floor(numel(xcseq{8})/2)+1;
p2=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-',lags{8}(zerolag8:zerolag8+30),xcseq{8}(zerolag8:zerolag8+30),'k-');
p2(1).LineWidth=0.5;
p2(2).LineWidth=0.5;
p2(3).LineWidth=0.5;
p2(4).LineWidth=0.5;
p2(5).LineWidth=0.75;
p2(5).Marker = '*';
p2(6).LineWidth=0.75;
p2(6).Marker = 'o';
p2(7).LineWidth=0.75;
p2(7).Marker = '+';
p2(8).LineWidth=0.75;
p2(8).Marker = 'd';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(2)) '")']});

s3=subplot(1,5,3);
x1 = modwt(B3(:,1),'db4',8,'reflection');
y1 = modwt(B3(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p3=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p3(1).LineWidth=0.5;
p3(2).LineWidth=0.5;
p3(3).LineWidth=0.5;
p3(4).LineWidth=0.5;
p3(5).LineWidth=0.75;
p3(5).Marker = '*';
p3(6).LineWidth=0.75;
p3(6).Marker = 'o';
p3(7).LineWidth=0.75;
p3(7).Marker = '+';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(3)) '")']});

s4=subplot(1,5,4);
x1 = modwt(B4(:,1),'db4',8,'reflection');
y1 = modwt(B4(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
zerolag8 = floor(numel(xcseq{8})/2)+1;
p4=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-',lags{8}(zerolag8:zerolag8+30),xcseq{8}(zerolag8:zerolag8+30),'k-');
p4(1).LineWidth=0.5;
p4(2).LineWidth=0.5;
p4(3).LineWidth=0.5;
p4(4).LineWidth=0.5;
p4(5).LineWidth=0.75;
p4(5).Marker = '*';
p4(6).LineWidth=0.75;
p4(6).Marker = 'o';
p4(7).LineWidth=0.75;
p4(7).Marker = '+';
p4(8).LineWidth=0.75;
p4(8).Marker = 'd';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(4)) '")']});

s5=subplot(1,5,5);
x1 = modwt(B5(:,1),'db4',8,'reflection');
y1 = modwt(B5(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
zerolag8 = floor(numel(xcseq{8})/2)+1;
p5=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-',lags{8}(zerolag8:zerolag8+30),xcseq{8}(zerolag8:zerolag8+30),'k-');
p5(1).LineWidth=0.5;
p5(2).LineWidth=0.5;
p5(3).LineWidth=0.5;
p5(4).LineWidth=0.5;
p5(5).LineWidth=0.75;
p5(5).Marker = '*';
p5(6).LineWidth=0.75;
p5(6).Marker = 'o';
p5(7).LineWidth=0.75;
p5(7).Marker = '+';
p5(8).LineWidth=0.75;
p5(8).Marker = 'd';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(5)) '")']});

% position
s=get(s1, 'position');s(1)=0.04;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s1,'position',s); % [left bottom width height]
s=get(s2, 'position');s(1)=0.235;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s2,'position',s);
s=get(s3, 'position');s(1)=0.43;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s3,'position',s);
s=get(s4, 'position');s(1)=0.625;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s4,'position',s);
s=get(s5, 'position');s(1)=0.82;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s5,'position',s);

% add legend
Lgnd=legend('2-4','4-8','8-16','16-32','32-64','64-128','128-256','256-512','NumColumns',8,'FontSize',10)
Lgnd.Position(1) = 0.28;
Lgnd.Position(2) = 0.02;

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA04a','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA04a_bitcoin','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Fig A04b - wavelet cross-correlation sequence - ethereum
close all
clear
clc
cd('... path ...')
load('data02.mat')
% transformation and variable definition
n=size(eth,2)/3;
m=length(eth_datelabel)-1;
X=NaN(m,n);
j=1;
for i=1:1:n
    X(:,i)=diff(eth{:,j},1);
    names(:,i)=ethnames(:,j);
    j=j+3;
end
datelabel=eth_datelabel(2:end,:);
Y(:,1)=g_eth_bitfinex;
Y(:,2)=g_eth_bitstamp;
Y(:,3)=g_eth_bittrex;
Y(:,4)=g_eth_coinbase;
Y(:,5)=g_eth_kraken;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
cname='ethereum';
Cname='Ethereum';

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);
clear datelabel X Y A*

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
x1 = modwt(B1(:,1),'db4',8,'reflection');
y1 = modwt(B1(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p1=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p1(1).LineWidth=0.5;
p1(2).LineWidth=0.5;
p1(3).LineWidth=0.5;
p1(4).LineWidth=0.5;
p1(5).LineWidth=0.75;
p1(5).Marker = '*';
p1(6).LineWidth=0.75;
p1(6).Marker = 'o';
p1(7).LineWidth=0.75;
p1(7).Marker = '+';
xlabel('Lead (days)');
ylabel('Correlation');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(1)) '")']});

s2=subplot(1,5,2);
x1 = modwt(B2(:,1),'db4',8,'reflection');
y1 = modwt(B2(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
p2=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-');
p2(1).LineWidth=0.5;
p2(2).LineWidth=0.5;
p2(3).LineWidth=0.5;
p2(4).LineWidth=0.5;
p2(5).LineWidth=0.75;
p2(5).Marker = '*';
p2(6).LineWidth=0.75;
p2(6).Marker = 'o';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(2)) '")']});

s3=subplot(1,5,3);
x1 = modwt(B3(:,1),'db4',8,'reflection');
y1 = modwt(B3(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p3=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p3(1).LineWidth=0.5;
p3(2).LineWidth=0.5;
p3(3).LineWidth=0.5;
p3(4).LineWidth=0.5;
p3(5).LineWidth=0.75;
p3(5).Marker = '*';
p3(6).LineWidth=0.75;
p3(6).Marker = 'o';
p3(7).LineWidth=0.75;
p3(7).Marker = '+';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(3)) '")']});

s4=subplot(1,5,4);
x1 = modwt(B4(:,1),'db4',8,'reflection');
y1 = modwt(B4(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p4=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p4(1).LineWidth=0.5;
p4(2).LineWidth=0.5;
p4(3).LineWidth=0.5;
p4(4).LineWidth=0.5;
p4(5).LineWidth=0.75;
p4(5).Marker = '*';
p4(6).LineWidth=0.75;
p4(6).Marker = 'o';
p4(7).LineWidth=0.75;
p4(7).Marker = '+';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(4)) '")']});

s5=subplot(1,5,5);
x1 = modwt(B5(:,1),'db4',8,'reflection');
y1 = modwt(B5(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p5=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p5(1).LineWidth=0.5;
p5(2).LineWidth=0.5;
p5(3).LineWidth=0.5;
p5(4).LineWidth=0.5;
p5(5).LineWidth=0.75;
p5(5).Marker = '*';
p5(6).LineWidth=0.75;
p5(6).Marker = 'o';
p5(7).LineWidth=0.75;
p5(7).Marker = '+';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(5)) '")']});

% position
s=get(s1, 'position');s(1)=0.04;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s1,'position',s); % [left bottom width height]
s=get(s2, 'position');s(1)=0.235;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s2,'position',s);
s=get(s3, 'position');s(1)=0.43;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s3,'position',s);
s=get(s4, 'position');s(1)=0.625;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s4,'position',s);
s=get(s5, 'position');s(1)=0.82;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s5,'position',s);

% add legend
Lgnd=legend('2-4','4-8','8-16','16-32','32-64','64-128','128-256','256-512','NumColumns',8,'FontSize',10)
Lgnd.Position(1) = 0.28;
Lgnd.Position(2) = 0.02;

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA04b','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA04b_ethereum','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Fig A04c - wavelet cross-correlation sequence - litecoin
close all
clear
clc
cd('... path ...')
load('data02.mat')
% transformation and variable definition
n=size(ltc,2)/3;
m=length(ltc_datelabel)-1;
X=NaN(m,n);
j=1;
for i=1:1:n
    X(:,i)=diff(ltc{:,j},1);
    names(:,i)=ltcnames(:,j);
    j=j+3;
end
datelabel=ltc_datelabel(2:end,:);
Y(:,1)=g_ltc_bitfinex;
Y(:,2)=g_ltc_bitstamp;
Y(:,3)=g_ltc_bittrex;
Y(:,4)=g_ltc_coinbase;
Y(:,5)=g_ltc_kraken;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
cname='litecoin';
Cname='Litecoin';

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);
clear datelabel X Y A*

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
x1 = modwt(B1(:,1),'db4',8,'reflection');
y1 = modwt(B1(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
zerolag8 = floor(numel(xcseq{8})/2)+1;
p1=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-',lags{8}(zerolag8:zerolag8+30),xcseq{8}(zerolag8:zerolag8+30),'k-');
p1(1).LineWidth=0.5;
p1(2).LineWidth=0.5;
p1(3).LineWidth=0.5;
p1(4).LineWidth=0.5;
p1(5).LineWidth=0.75;
p1(5).Marker = '*';
p1(6).LineWidth=0.75;
p1(6).Marker = 'o';
p1(7).LineWidth=0.75;
p1(7).Marker = '+';
p1(8).LineWidth=0.75;
p1(8).Marker = 'd';
xlabel('Lead (days)');
ylabel('Correlation');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(1)) '")']});

s2=subplot(1,5,2);
x1 = modwt(B2(:,1),'db4',8,'reflection');
y1 = modwt(B2(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p2=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p2(1).LineWidth=0.5;
p2(2).LineWidth=0.5;
p2(3).LineWidth=0.5;
p2(4).LineWidth=0.5;
p2(5).LineWidth=0.75;
p2(5).Marker = '*';
p2(6).LineWidth=0.75;
p2(6).Marker = 'o';
p2(7).LineWidth=0.75;
p2(7).Marker = '+';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(2)) '")']});

s3=subplot(1,5,3);
x1 = modwt(B3(:,1),'db4',8,'reflection');
y1 = modwt(B3(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p3=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p3(1).LineWidth=0.5;
p3(2).LineWidth=0.5;
p3(3).LineWidth=0.5;
p3(4).LineWidth=0.5;
p3(5).LineWidth=0.75;
p3(5).Marker = '*';
p3(6).LineWidth=0.75;
p3(6).Marker = 'o';
p3(7).LineWidth=0.75;
p3(7).Marker = '+';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(3)) '")']});

s4=subplot(1,5,4);
x1 = modwt(B4(:,1),'db4',8,'reflection');
y1 = modwt(B4(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
p4=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-');
p4(1).LineWidth=0.5;
p4(2).LineWidth=0.5;
p4(3).LineWidth=0.5;
p4(4).LineWidth=0.5;
p4(5).LineWidth=0.75;
p4(5).Marker = '*';
p4(6).LineWidth=0.75;
p4(6).Marker = 'o';
p4(7).LineWidth=0.75;
p4(7).Marker = '+';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(4)) '")']});

s5=subplot(1,5,5);
x1 = modwt(B5(:,1),'db4',8,'reflection');
y1 = modwt(B5(:,2),'db4',8,'reflection');
[xcseq,xcseqci,lags] = modwtxcorr(y1,x1,'db4','reflection');
zerolag1 = floor(numel(xcseq{1})/2)+1;
zerolag2 = floor(numel(xcseq{2})/2)+1;
zerolag3 = floor(numel(xcseq{3})/2)+1;
zerolag4 = floor(numel(xcseq{4})/2)+1;
zerolag5 = floor(numel(xcseq{5})/2)+1;
zerolag6 = floor(numel(xcseq{6})/2)+1;
zerolag7 = floor(numel(xcseq{7})/2)+1;
zerolag8 = floor(numel(xcseq{8})/2)+1;
p5=plot(lags{1}(zerolag1:zerolag1+30),xcseq{1}(zerolag1:zerolag1+30),'k:',lags{2}(zerolag2:zerolag2+30),xcseq{2}(zerolag2:zerolag2+30),'k-.', ...
    lags{3}(zerolag3:zerolag3+30),xcseq{3}(zerolag3:zerolag3+30),'k--',lags{4}(zerolag4:zerolag4+30),xcseq{4}(zerolag4:zerolag4+30),'k-', ...
    lags{5}(zerolag5:zerolag5+30),xcseq{5}(zerolag5:zerolag5+30),'k-',lags{6}(zerolag6:zerolag6+30),xcseq{6}(zerolag6:zerolag6+30),'k-', ...
    lags{7}(zerolag7:zerolag7+30),xcseq{7}(zerolag7:zerolag7+30),'k-',lags{8}(zerolag8:zerolag8+30),xcseq{8}(zerolag8:zerolag8+30),'k-');
p5(1).LineWidth=0.5;
p5(2).LineWidth=0.5;
p5(3).LineWidth=0.5;
p5(4).LineWidth=0.5;
p5(5).LineWidth=0.75;
p5(5).Marker = '*';
p5(6).LineWidth=0.75;
p5(6).Marker = 'o';
p5(7).LineWidth=0.75;
p5(7).Marker = '+';
p5(8).LineWidth=0.75;
p5(8).Marker = 'd';
xlabel('Lead (days)');
ylabel(' ');
ylim([-1 1])
title({['SVI diff ("' cname ' ' cell2mat(names(5)) '")']});

% position
s=get(s1, 'position');s(1)=0.04;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s1,'position',s); % [left bottom width height]
s=get(s2, 'position');s(1)=0.235;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s2,'position',s);
s=get(s3, 'position');s(1)=0.43;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s3,'position',s);
s=get(s4, 'position');s(1)=0.625;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s4,'position',s);
s=get(s5, 'position');s(1)=0.82;s(3)=0.165;s(2)=0.25;s(4)=0.69;set(s5,'position',s);

% add legend
Lgnd=legend('2-4','4-8','8-16','16-32','32-64','64-128','128-256','256-512','NumColumns',8,'FontSize',10)
Lgnd.Position(1) = 0.28;
Lgnd.Position(2) = 0.02;

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA04c','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA04c_litecoin','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

